using UnityEngine;

public class AnimalTrap : MonoBehaviour
{
	public GameObject open;

	public GameObject close;

	public AudioSource audioSource;

	public AudioClip closeAudio;

	public Collider2D col;

	public Tool tool;

	public bool isTrigged;

	[Header("Runtime")]
	public Zombie trappedZombie;

	private void Update()
	{
		if (trappedZombie == null && !isTrigged)
		{
			col.enabled = tool.placed;
		}
	}

	private void OnEnable()
	{
		CampaignGame.Instance.onWaveStart.AddListener(Open);
	}

	private void OnDisable()
	{
		CampaignGame.Instance.onWaveStart.RemoveListener(Open);
	}

	private void OnCollisionEnter2D(Collision2D collision)
	{
		if (tool.placed && !trappedZombie && collision.collider.TryGetComponent<Zombie>(out var component) && !component.animalTrap)
		{
			Catch(component);
			component.Trapped(this);
		}
	}

	public void Catch(Zombie zombie)
	{
		trappedZombie = zombie;
		open.SetActive(!trappedZombie);
		close.SetActive(trappedZombie);
		audioSource.PlayOneShot(closeAudio);
		col.enabled = false;
		isTrigged = true;
	}

	public void Open()
	{
		trappedZombie = null;
		open.SetActive(!trappedZombie);
		close.SetActive(trappedZombie);
		col.enabled = true;
		isTrigged = false;
	}
}
